﻿@using CleanArchitecture.Blazor.Application.Features.LoginAudits.DTOs
@using CleanArchitecture.Blazor.Application.Features.LoginAudits.Queries.GetRiskSummaryStatistics
@using CleanArchitecture.Blazor.Domain.Enums
@using MudBlazor

@inject IStringLocalizer<SecurityRiskDashboard> L

<MudCard Class="mb-4">
    <MudCardContent>
        <MudStack Row Justify="Justify.SpaceBetween" AlignItems="AlignItems.Center" Class="mb-3">
            <MudStack Row AlignItems="AlignItems.Center" Spacing="2">
                <MudIcon Icon="@Icons.Material.Filled.Dashboard" Color="Color.Primary" Size="Size.Large" />
                <MudText Typo="Typo.h6" Color="Color.Primary">@L["Security Risk Analysis Dashboard"]</MudText>
            </MudStack>
            <MudStack Row AlignItems="AlignItems.Center" Spacing="2">
                <MudTooltip Text="Refresh Dashboard">
                    <MudIconButton Icon="@Icons.Material.Filled.Refresh" 
                                  Color="Color.Primary" 
                                  Size="Size.Small" 
                                  OnClick="LoadStatistics" />
                </MudTooltip>
            </MudStack>
        </MudStack>

        @if (_loading)
        {
            <MudProgressLinear Color="Color.Primary" Indeterminate="true" Class="mb-4" />
        }
        else if (_statistics != null)
        {
            <MudGrid>
                <!-- Total Users -->
                <MudItem xs="12" sm="6" md="3">
                    <MudPaper Class="pa-4 text-center" Style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;" Elevation="4">
                        <MudIcon Icon="@Icons.Material.Filled.People" Size="Size.Large" Color="Color.Inherit" Class="mb-2" />
                        <MudText Typo="Typo.h4" Color="Color.Inherit">@_statistics.TotalUsers</MudText>
                        <MudText Typo="Typo.body2" Color="Color.Inherit">@L["Total Users"]</MudText>
                    </MudPaper>
                </MudItem>

                <!-- Low Risk -->
                <MudItem xs="12" sm="6" md="3">
                    <MudPaper Class="pa-4 text-center" Style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white;" Elevation="4">
                        <MudIcon Icon="@Icons.Material.Filled.CheckCircle" Size="Size.Large" Color="Color.Inherit" Class="mb-2" />
                        <MudText Typo="Typo.h4" Color="Color.Inherit">@_statistics.LowRiskUsers</MudText>
                        <MudText Typo="Typo.body2" Color="Color.Inherit">@L["Low Risk"]</MudText>
                       
                    </MudPaper>
                </MudItem>

                <!-- Medium Risk -->
                <MudItem xs="12" sm="6" md="3">
                    <MudPaper Class="pa-4 text-center" Style="background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%); color: white;" Elevation="4">
                        <MudIcon Icon="@Icons.Material.Filled.Warning" Size="Size.Large" Color="Color.Inherit" Class="mb-2" />
                        <MudText Typo="Typo.h4" Color="Color.Inherit">@_statistics.MediumRiskUsers</MudText>
                        <MudText Typo="Typo.body2" Color="Color.Inherit">@L["Medium Risk"]</MudText>
                        
                    </MudPaper>
                </MudItem>

                <!-- High & Critical Risk -->
                <MudItem xs="12" sm="6" md="3">
                    <MudPaper Class="pa-4 text-center" Style="background: linear-gradient(135deg, #ff9a9e 0%, #fecfef 100%); color: #333;" Elevation="4">
                        <MudIcon Icon="@Icons.Material.Filled.Error" Size="Size.Large" Color="Color.Error" Class="mb-2" />
                        <MudText Typo="Typo.h4" Color="Color.Error">@(_statistics.HighRiskUsers + _statistics.CriticalRiskUsers)</MudText>
                        <MudText Typo="Typo.body2" Color="Color.Error">@L["High Risk"]</MudText>
                       
                    </MudPaper>
                </MudItem>

       

              
            </MudGrid>
        }
        else
        {
            <MudAlert Severity="Severity.Info" Variant="Variant.Outlined">
                <MudText>@L["No security analysis data available"]</MudText>
            </MudAlert>
        }
    </MudCardContent>
</MudCard>

@code {
    private RiskSummaryStatisticsDto? _statistics;
    private bool _loading = true;

    protected override async Task OnInitializedAsync()
    {
        await LoadStatistics();
    }

    private async Task LoadStatistics()
    {
        try
        {
            _loading = true;
            var query = new GetRiskSummaryStatisticsQuery();
            _statistics = await Mediator.Send(query);
        }
        catch (Exception ex)
        {
            // Log error
            Console.WriteLine($"Error loading security statistics: {ex.Message}");
        }
        finally
        {
            _loading = false;
        }
    }

    private int GetPercentage(int value, int total)
    {
        return total > 0 ? (int)Math.Round((double)value / total * 100) : 0;
    }

    private Color GetRiskColor(double score)
    {
        return score switch
        {
            >= 80 => Color.Error,    // Critical
            >= 60 => Color.Error,    // High
            >= 40 => Color.Warning,  // Medium
            _ => Color.Success       // Low
        };
    }
}
